/*
 *   Servlet for surfacing syndicated content
 *   Copyright (C) 2011 Glenn Engstrand
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.dynamicalsoftware.news.publisher;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.dynamicalsoftware.news.NewsPublisher;

import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.OperationType;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.HConsistencyLevel;
import me.prettyprint.hector.api.ConsistencyLevelPolicy;

/**
 * publish displayed references to a cassandra db
 * @author glenn
 *
 */
public class CassandraPublisher implements NewsPublisher {
	
	private Keyspace keyspaceOperator = null;
	private Cluster cluster = null;
	private static final Logger log = Logger.getLogger(CassandraPublisher.class.getCanonicalName());

	/**
	 * retrieve cassandra keyspace
	 * @return cached cassandra keyspace
	 */
	private Keyspace getKeyspace() {
		if (keyspaceOperator == null) {
	        cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160");
	        keyspaceOperator = HFactory.createKeyspace("media", cluster, new MyConsistencyLevelPolicy());
		}
		return keyspaceOperator;
	}
	
	/**
	 * retrieve cassandra mutator
	 * @param key - cassandra key
	 * @return cached cassandra key
	 */
	private Mutator<String> getMutator(String key) {
		Mutator<String> retVal = null;
        try {
        	retVal = HFactory.createMutator(getKeyspace(), StringSerializer.get());
        } catch (HectorException e) {
            log.log(Level.SEVERE, "cannot create cassandra mutator\n", e);
        }
		return retVal;
	}
	
	public void publish(String key, String title, String link, String description) {
		String diagnostic = null;;
		Mutator<String> mutator = getMutator(key);
        try {
        	if (link != null) {
            	diagnostic = "link";
	            mutator.addInsertion(key, "news", HFactory.createStringColumn("link", link));
        	}
        	if (title != null) {
	            diagnostic = "title";
	            mutator.addInsertion(key, "news", HFactory.createStringColumn("title", title));
        	}
        	if (description != null) {
	            diagnostic = "description";
	            mutator.addInsertion(key, "news", HFactory.createStringColumn("description", description));
        	}
        	diagnostic = "execute";
        	mutator.execute();
        	log.log(Level.INFO, "successfully written key: " + key);
        } catch (Exception e) {
            log.log(Level.SEVERE, "error inserting " + diagnostic + "\n", e);
        }
	}

	public void close() {
		if (cluster != null) {
			cluster.getConnectionManager().shutdown();
		}
	}

	/**
	 * override hector default of quorem to use one instead
	 * @author glenn
	 *
	 */
	class MyConsistencyLevelPolicy implements ConsistencyLevelPolicy {

		public HConsistencyLevel get(OperationType ot) {
			return HConsistencyLevel.ONE;
		}
		
		public HConsistencyLevel get(OperationType ot, String arg1) {
			return get(ot);
		}
		
	}
}
